package com.stillalive.springtrans.controller;

import com.stillalive.springtrans.service.LogService;
import com.stillalive.springtrans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user2")
@RestController
public class UserController2 {
    @Autowired
    private UserService userService;

    @Autowired
    private LogService logService;

    @Transactional
    @RequestMapping("/register")
    public String registry(String name, String password){
        userService.registerUser(name, password);
        logService.registerUser(name, password);

//        try{
//            int a = 10/0;
//        }catch (ArithmeticException e){
//            log.error(e.getMessage());
//            // 发生异常时手动回滚事务
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }
        return "success";
    }

    @RequestMapping("/test1")
    @Transactional
    public String test1(String name,String password){
//        try{
//            int err = 10/0;
//        }catch(Exception e){
//            e.printStackTrace();
//        }

        userService.registerUser(name, password);
        logService.registerUser(name, password);

        // System.out.println("test1");
        return "success";
    }
}

